<template>
  <div>
    <div class="bg-white">
      <div class="container">
        <div
          class="row justify-content-center align-items-center d-flex vh-100"
        >
          <div class="col-md-4 mx-auto">
            <div class="osahan-login py-4">
              <div class="text-center mb-4">
                <a href="#"><img src="@/assets/img/logorl.png" width="300" alt="" /></a>
                <p class="text-muted mt-2">
                  我们为什么如此热爱未来，因为我们将生活在那里。
                </p>
              </div>
              <div>
                <div class="form-row">
                  <div class="col">
                    <div class="form-group">
                      <label class="mb-1">手机/账号/邮件</label>
                      <div class="position-relative icon-form-control">
                        <i class="el-icon-user position-absolute"></i>
                        <input v-model="account" type="text" class="form-control" />
                      </div>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label class="mb-1">密码</label>
                  <div class="position-relative icon-form-control">
                    <i class="el-icon-lock position-absolute"></i>
                    <input v-model="password1" type="password" class="form-control" />
                  </div>
                </div>
                <div class="form-group">
                  <label class="mb-1">重新输入密码</label>
                  <div class="position-relative icon-form-control">
                    <i class="el-icon-lock position-absolute"></i>
                    <input v-model="password2" type="password" class="form-control" />
                  </div>
                </div>
                <div class="form-row">
                  <div class="col">
                    <div class="form-group">
                      <label class="mb-1">昵称</label>
                      <div class="position-relative icon-form-control">
                        <i class="el-icon-lollipop position-absolute"></i>
                        <input v-model="name" type="text" class="form-control" />
                      </div>
                    </div>
                  </div>
                </div>
                <div class="form-group">
                  <label class="mb-1">头像</label>
                  <div class="position-relative  header">
                    <el-upload
                      :on-change="getFile"
                      action="#"
                      :file-list="file_list"
                      :class="file_list.length >= 1 ? 'a' : ''"
                      list-type="picture-card"
                      :auto-upload="false">
                      <i slot="default" class="el-icon-plus"></i>
                      <div slot="file" slot-scope="{file}">
                        <img
                          class="el-upload-list__item-thumbnail"
                          :src="file.url" alt=""
                        >
                        <span class="el-upload-list__item-actions">
                          <span
                            class="el-upload-list__item-delete"
                            @click="handleRemove(file)"
                          >
                            <i class="el-icon-delete"></i>
                          </span>
                        </span>
                      </div>
                    </el-upload>
                  </div>
                </div>
                <div class="form-group">
                  <!-- <label class="mb-1"
                  > <a href="#">User Agreement</a>,
                    <a href="#">Privacy Policy</a>, and
                    <a href="#">Cookie Policy</a>.</label
                  > -->
                </div>
                <button
                  @click="register"
                  class="btn btn-primary btn-block text-uppercase"
                  type="submit"
                >
                  注册
                </button>
                <!-- <div class="text-center mt-3 border-bottom pb-3">
                  <p class="small text-muted">Or login with</p>
                  <div class="row">
                    <div class="col-4">
                      <button
                        type="button"
                        class="btn btn-sm btn-outline-instagram btn-block"
                      >
                        <i class="feather-instagram"></i> Instagram
                      </button>
                    </div>
                    <div class="col-4">
                      <button
                        type="button"
                        class="btn btn-sm btn-outline-linkedin btn-block"
                      >
                        <i class="feather-linkedin"></i> Linkedin
                      </button>
                    </div>
                    <div class="col-4">
                      <button
                        type="button"
                        class="btn btn-sm btn-outline-facebook btn-block"
                      >
                        <i class="feather-facebook"></i> Facebook
                      </button>
                    </div>
                  </div>
                </div> -->
                <div class="py-3 d-flex align-item-center">
                  <div class="text-primary" @click="forget">忘记密码?</div>
                  <span class="ml-auto" @click="login">
                    已经有账号?
                    <a class="font-weight-bold" href="#"
                    >登录</a
                    ></span
                  >
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      account: "",
      password1: '',
      password2: "",
      name: "",
      base64: '',
      file_list: [],
      registerStore: this.newStore(),
    };
  },
  components: {},
  methods: {
    forget() {
      alert('功能开发中');
    },
    login() {
      this.$router.push('/login');
    },
    register() {
      if (this.account === '') {
        this.$message({
          showClose: true,
          message: '账号不能为空',
          type: 'warning',
        });
        return;
      }
      const re = /^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/;
      if (this.account !== '' && !re.test(this.account)) {
        this.$message({
          showClose: true,
          message: '请输入正确的邮箱账号',
          type: 'warning',
        });
        return;
      }
      if (this.password1 === "" || this.password2 === "") {
        this.$message({
          showClose: true,
          message: '密码不能为空',
          type: 'warning',
        });
        return;
      }
      if (this.password1 !== this.password2) {
        this.$message({
          showClose: true,
          message: '两次输入密码不同！',
          type: 'warning',
        });
        return;
      }
      if (this.base64 === '') {
        this.$message({
          showClose: true,
          message: '请上传头像！',
          type: 'warning',
        });
        return;
      }
      this.spost(this.registerStore, '/register', {
        account: this.account,
        password: this.password1,
        name: this.name,
        userHeard: this.base64,
      }).then(() => {
        this.$message({
          showClose: true,
          message: '注册成功！',
          type: 'success',
        });
        this.$router.push('/login');
      });
    },
    getFile(file) {
      this.file_list = [file];
      this.getBase64(file.raw).then((res) => {
        this.base64 = res;
      });
    },
    // 删除图片
    handleRemove() {
      this.file_list = [];
      this.base64 = '';
    },
    // 64
    getBase64(file) {
      return new Promise((resolve, reject) => {
        const reader = new FileReader();
        let imgResult = '';
        reader.readAsDataURL(file);
        reader.onload = () => {
          imgResult = reader.result;
        };
        reader.onerror = (error) => {
          reject(error);
        };
        reader.onloadend = () => {
          resolve(imgResult);
        };
      });
    },
  },
};
</script>

<style scoped lang="scss">
 /deep/.el-upload--picture-card {
    line-height: 70px;
    height: 60px;
    width: 60px;
 }
  :deep(.el-upload-list__item-actions, ) {
    line-height: 60px;
    height: 60px;
    width: 60px;
  }
  /deep/ .el-upload-list__item {
    line-height: 60px;
    height: 60px;
    width: 60px;
  }
  /deep/ .el-upload-list__item-thumbnail {
    height: 60px;
    width: 60px;
  }
  .a {
    /deep/ .el-upload--picture-card {
      display: none;
    }
  }
</style>
